package com.financial.service.yspay.service.query.impl;

import com.aliyun.openservices.shade.com.alibaba.fastjson.JSON;
import com.financial.service.yspay.connector.SysconfigFeign;
import com.financial.service.yspay.dto.FinancialYsPayDTO;
import com.financial.service.yspay.service.FinancialYsPayBuildRequestParaService;
import com.financial.service.yspay.service.FinancialYsPayVerifyJsonSignService;
import com.financial.service.yspay.service.query.FinancialYsPayQueryService;
import com.financial.service.yspay.utils.DateUtils;
import com.financial.service.yspay.utils.HttpUtils;
import com.financial.service.yspay.utils.JsonUtils;
import com.financial.service.yspay.utils.apiresult.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author WANGNING
 * @Email wyn20140226@aliyun.com
 * @Description //银盛支付查询
 * @Date 2019/11/22 15:11
 */
@Service
public class FinancialYsPayQueryServiceImpl implements FinancialYsPayQueryService {

    public static final Logger logger = LoggerFactory.getLogger(FinancialYsPayQueryServiceImpl.class);

    @Autowired
    private SysconfigFeign sysconfigFeign;

    @Autowired
    private FinancialYsPayBuildRequestParaService ysBuildRequestParaService;

    @Value("${ysPayQueryMethod}")
    private String ysPayQueryMethod;

    @Autowired
    private FinancialYsPayVerifyJsonSignService ysVerifyJsonSignService;

    @Override
    public ApiResult pay(String requestParam) {
        logger.info("FinancialYsPayQueryServiceImpl.pay()---获取支付查询参数" + requestParam);
        String ysPayJson = sysconfigFeign.getServiceYsConfig();
        FinancialYsPayDTO ysPayDTO =JsonUtils.parseObject(ysPayJson);
        Map<String, String> param = new HashMap<String, String>();
        param.put("method", ysPayQueryMethod);
        param.put("partner_id", ysPayDTO.getPARTNER_ID());
        param.put("timestamp", DateUtils.getNowTimeStr());
        param.put("charset", ysPayDTO.getDEFAULT_CHARSET());
        param.put("sign_type", ysPayDTO.getSIGN_ALGORITHM());
        param.put("version", ysPayDTO.getVERSION());
        param.put("biz_content", requestParam);
        /*签名*/
        String content = null;
        Map<String, String> sPara = ysBuildRequestParaService.buildRequestPara(param, ysPayDTO);
        try {
            /*向银盛发起请求*/
            String returnString = HttpUtils.httpsSend(ysPayDTO.getYSEPAY_GATEWAY_URL_SEARCH(), sPara);
            /*请求回执处理*/
            Map<String, Object> resultMap = (Map<String, Object>) JSON.parse(returnString);
            Object obj = resultMap.get("ysepay_online_trade_query_response");
            if (obj != null) {
                Map<String, String> ysepay_online_trade_query_response = (Map) JSON.parseObject(obj.toString());
                int ss = returnString.indexOf("ysepay_online_trade_query_response");
                /*截出验签内容*/
                content = returnString.substring(ss);
                content = content.replace("ysepay_online_trade_query_response", "");
                content = content.substring(2, content.length() - 1);
                boolean verifyResult = ysVerifyJsonSignService.verifyJsonSign(resultMap.get("sign").toString(), content, "utf-8", ysPayDTO);
                logger.info("FinancialYsPayQueryServiceImpl.pay()---银盛支付查询接口验签：" + verifyResult);
                logger.info("FinancialYsPayQueryServiceImpl.pay()---银盛支持查询回执信息" + content);
                return ApiResult.success(content);
            }
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("FinancialYsPayQueryServiceImpl.pay()---银盛支持查询接口" + e.getMessage());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            logger.error("FinancialYsPayQueryServiceImpl.pay()---银盛支持查询接口" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("FinancialYsPayQueryServiceImpl.pay()---银盛支持查询接口" + e.getMessage());
        }
        return ApiResult.failure(content);
    }
}
